package com.fenghuajueli.module_home

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.lib_pictureselect.PhotoSelectorBuilder
import com.fenghuajueli.lib_pictureselect.config.PhotoConfig
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyV3Helper
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.module_home.databinding.ActivityV3adTestBinding
import com.sy.lib_base_ad.AdErrorEnum
import com.sy.lib_base_ad.SimpleAdListener
import com.sy.module_ad_switch_manager.AdShowHelper
import com.sy.module_ad_switch_manager.listener.IPayEventCallback

/**
 * Create by linyue
 * Time:2023/2/17 11:02
 */
class V3AdTestActivity : AppCompatActivity() {
    private val TAG = "V3AdTestActivity"
    private lateinit var binding: ActivityV3adTestBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityV3adTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initPayClick()
        initAdShow()
        initAdShow2()
        initClickFunction()
    }

    private fun initPayClick() {
        binding.tvNeedPay.setOnClickListener {
            AdShowHelper.payEvent(
                SwitchKeyUtils.getPayStatus(),
                UserInfoUtils.getInstance().isVip, this,
                object : SimpleAdListener() {
                    //广告的回调在这里
                    override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                        ToastUtils.showShort("广告加载失败")
                    }

                    override fun onAdClose() {
                        pickImage()
                    }

                    override fun onAdReward(rewardValue: String?, bundle: Bundle?) {
                        ToastUtils.showShort("获得奖励")
                    }
                },
                object : IPayEventCallback {
                    override fun onGotoBuyVip() {
                        PublicFunction.goBuyVip()
                    }

                    override fun onCloseDialog() {

                    }

                    override fun onNormalExecute() {
                        pickImage()
                    }

                }
            )
        }
    }

    private fun initClickFunction() {
        binding.tvP1.setOnClickListener {
            AdShowHelper.adEvent(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), 1, this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    pickImage()
                }
            })
        }
        binding.tvP2.setOnClickListener {
            AdShowHelper.adEvent(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), 2, this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    pickImage()
                }
            })
        }
        binding.tvP3.setOnClickListener {
            AdShowHelper.adEvent(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), 3, this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    pickImage()
                }
            })
        }
        binding.tvP4.setOnClickListener {
            AdShowHelper.adEvent(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), 4, this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    pickImage()
                }
            })
        }
        binding.tvP5.setOnClickListener {
            AdShowHelper.adEvent(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), 5, this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    pickImage()
                }
            })
        }
    }

    private val SimpleAdListener = object : SimpleAdListener() {
        override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
            ToastUtils.showShort("不符合广告加载条件或广告加载失败")
        }

        override fun onAdClose() {
            ToastUtils.showShort("广告关闭")
        }
    }

    private fun initAdShow() {
        binding.tvShowBannerAd.setOnClickListener {
            AdShowHelper.showBannerAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, binding.flAdBannerContainer, "banner", listener = SimpleAdListener)
        }
        binding.tvShowInfoAd.setOnClickListener {
            AdShowHelper.showInformationAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, binding.flAdContainer, "flow", listener = SimpleAdListener)
        }
        binding.tvShowSplashAd.setOnClickListener {
            AdShowHelper.showSplashAd(SwitchKeyV3Helper.getAdSwitch(), this, binding.flAdSplashContainer) {
                binding.flAdSplashContainer.removeAllViews()
            }
        }
        binding.tvShowInsertAd.setOnClickListener {
            AdShowHelper.showInsertAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, "insert", listener = SimpleAdListener)
        }
        binding.tvShowRewardAd.setOnClickListener {
            AdShowHelper.showRewardAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    ToastUtils.showShort("广告关闭")
                }

                override fun onAdReward(rewardValue: String?, bundle: Bundle?) {
                    ToastUtils.showShort("获得奖励")
                }
            })
        }
        binding.tvShowFullAd.setOnClickListener {
            AdShowHelper.showFullAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, SimpleAdListener)
        }
    }

    private fun initAdShow2() {
        binding.tvShowFullAd1.setOnClickListener {
            testInsert = false
            testFull = true
            AdShowHelper.showFullAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    ToastUtils.showShort("广告关闭")
                    pickImage()
                }
            })
        }

        binding.tvShowInsertAd1.setOnClickListener {
            testInsert = true
            testFull = false
            AdShowHelper.showInsertAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, "insert", object : SimpleAdListener() {
                override fun onAdLoadError(errorCode: AdErrorEnum, errorMessage: String?) {
                    ToastUtils.showShort("不符合广告加载条件或广告加载失败")
                }

                override fun onAdClose() {
                    ToastUtils.showShort("广告关闭")
                    pickImage()
                }
            })
        }
    }

    private var testInsert = false
    private var testFull = false
    override fun onResume() {
        super.onResume()
        AdShowHelper.showBannerAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, binding.flAdBannerContainer, "banner", listener = SimpleAdListener)
        AdShowHelper.showInformationAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, binding.flAdContainer, "flow", listener = SimpleAdListener)

        if (testInsert) {
            AdShowHelper.showInsertAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, "insert", SimpleAdListener)
        }

        if (testFull) {
            AdShowHelper.showFullAd(UserInfoUtils.getInstance().isVip, SwitchKeyV3Helper.getAdSwitch(), this, SimpleAdListener)
        }
    }


    private fun pickImage() {
        PhotoSelectorBuilder.builder(this)
            .mode(PhotoConfig.Mode.PHOTO)
            .isCopyToPrivate(false)
            .maxCount(9)
            .minCount(1)
            .listener { data -> LogUtils.json(data) }
    }
}